using System;
using SmartPeople.Framework.SecuritySpace.Provider.Tests;
using SmartPeople.Framework.SecuritySpace.Tests;

namespace SmartPeople.Framework.SecuritySpace.Provider.Tests
{
    public class TestsHelper
    {
        private static ISecuritySpaceProviderFactory securitySpaceProviderFactory;


        static TestsHelper()
        {
            SecuritySpaceProviderFactory = new XPOProviderFactory();
        }

        public static ISecuritySpaceProviderFactory SecuritySpaceProviderFactory
        {
            get { return securitySpaceProviderFactory; }
            set { securitySpaceProviderFactory = value; }
        }

        #region GetSecuritySpace

        public static ISecuritySpace GetSecuritySpace()
        {
            if (SecuritySpaceProviderFactory == null)
                throw new ArgumentNullException("SecuritySpaceProviderFactory",
                                                "Please call SetSecuritySpaceProviderFactory First.");

            return securitySpaceProviderFactory.GetSecuritySpace();
        }

        public static ISecuritySpace GetSecuritySpace(string applicationName)
        {
            if (SecuritySpaceProviderFactory == null)
                throw new ArgumentNullException("SecuritySpaceProviderFactory",
                                                "Please call SetSecuritySpaceProviderFactory First.");

            return securitySpaceProviderFactory.GetSecuritySpace(applicationName);
        }

        #endregion

        #region GetSecurityPermissions

        public static ISecurityPermissions GetSecurityPermissions()
        {
            return GetSecuritySpace().SecurityPermissions;
        }

        #endregion

        #region Initialize_SecuredTypes

        public static void Initialize_SecuredTypes(ISecuritySpace securitySpace)
        {
            ISecuritySecuredType securedFolder = securitySpace.SecuredTypes.CreateType(TestSecuredTypes.Folder);
            securedFolder.Actions.CreateAction(TestActions.List);
            securedFolder.Actions.CreateAction(TestActions.Read);
            securedFolder.Actions.CreateAction(TestActions.Create);
            securedFolder.Actions.CreateAction(TestActions.Modify);
            securedFolder.Actions.CreateAction(TestActions.Delete);

            ISecuritySecuredType securedFile = securitySpace.SecuredTypes.CreateType(TestSecuredTypes.UnknownFileType);
            securedFile.Actions.CreateAction(TestActions.Read);
            securedFile.Actions.CreateAction(TestActions.Create);
            securedFile.Actions.CreateAction(TestActions.Modify);
            securedFile.Actions.CreateAction(TestActions.Delete);

            securedFile.States.CreateState(TestStates.New);
            securedFile.States.CreateState(TestStates.Normal);
            securedFile.States.CreateState(TestStates.Protected);

            ISecuritySecuredType baseSecuredProperty = securitySpace.SecuredTypes.CreateType("BaseProperty");
            baseSecuredProperty.Actions.CreateAction(TestActions.Get);
            baseSecuredProperty.Actions.CreateAction(TestActions.Set);
        }

        #endregion

        #region Initialize_Users

        public static void Initialize_Users(ISecuritySpace securitySpace)
        {
            securitySpace.Users.CreateUser(TestUsers.System, "thintsystem@acme.com");
            securitySpace.Users.CreateUser(TestUsers.Administrator, "thintadmin@acme.com");
            securitySpace.Users.CreateUser(TestUsers.Guest, "thintguest@acme.com");
            securitySpace.Users.CreateUser(TestUsers.User1, "thintuser1@acme.com");
            securitySpace.Users.CreateUser(TestUsers.User2, "thintuser2@acme.com");
            securitySpace.Users.CreateUser(TestUsers.User3, "thintuser3@acme.com");
        }

        #endregion

        #region Initialize_Roles_Users

        public static void Initialize_Roles_Users(ISecuritySpace securitySpace)
        {
            TestRoles.CreateAllTestRoles(securitySpace);
            Initialize_Users(securitySpace);
        }

        #endregion

        #region Initialize_SecuredDocuments

        public static void Initialize_SecuredDocuments(ISecuritySpace securitySpace)
        {
            ISecuritySecuredType securedFolder = securitySpace.SecuredTypes.GetSecuredType(TestSecuredTypes.Folder);
            ISecuritySecuredType securedFile =
                securitySpace.SecuredTypes.GetSecuredType(TestSecuredTypes.UnknownFileType);
            securitySpace.SecuredDocuments.SetDefaultPathType(securedFolder);
            securitySpace.SecuredDocuments.SetDefaultDocumentType(securedFile);
        }

        #endregion
    }
}